/***
 * 
 *  Title:模态窗口管理
 *           
 *  
 *  Description:
 *         功能：UI遮罩管理器
 *         
 *  Author:jack
 *  
 *  Data:2023-12-28
 *  
 *  Modify:
 *  
 *  
 *  
 */
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class UIMaskMgr : MonoBehaviour
{
    private static UIMaskMgr _Instance = null;
    //UI根节点对象
    private GameObject _GoCanvasRoot = null;
    //UI脚本节点对象
    private Transform _TraUIScriptsNode = null;
    //顶层面板
    private GameObject _GoTopPanel = null;
    //遮罩面板
    private GameObject _GoMaskPanel = null;

    public static UIMaskMgr GetInstance()
    {
        if (_Instance == null)
        {
            _Instance = new GameObject("_UIMaskMgr").AddComponent<UIMaskMgr>();
        }
        return _Instance;
    }

    private void Awake()
    {
        //得到UI节点对象。脚本节点对象
        _GoCanvasRoot = GameObject.FindGameObjectWithTag(SysDefine.SYS_TAG_CANVAS);
        _TraUIScriptsNode = UnityHelper.FindTheChildNode(_GoCanvasRoot, SysDefine.SYS_SCRIPTMANAGER_NODE);
        //把脚本实例 添加为脚本节点的子节点
        UnityHelper.AddChildNodeToParentNode(_TraUIScriptsNode, this.gameObject.transform);
        //得到“顶层面板”、“遮罩面板”
        _GoTopPanel = _GoCanvasRoot;
        _GoMaskPanel = UnityHelper.FindTheChildNode(_GoCanvasRoot, "_UIMaskPanel").gameObject;
    }

    /// <summary>
    /// 设置遮罩状态
    /// </summary>
    /// <param name="goDisplarUIForms">需要显示的UI窗体</param>
    /// <param name="lucenyType">显示透明度属性</param>
    public void setMaskWindow(GameObject goDisplarUIForms, UIFormLucenyType lucenyType = UIFormLucenyType.Lucency)
    {
        //顶层窗口下移
        _GoTopPanel.transform.SetAsLastSibling();
        //启用遮罩窗体以及设置透明度
        switch (lucenyType)
        {
            case UIFormLucenyType.Lucency:                                  //完全透明，不能穿透
                _GoMaskPanel.SetActive(true);
                Color newColor1 = new Color(255, 255, 255, 0);
                _GoMaskPanel.GetComponent<Image>().color = newColor1;
                break;
            case UIFormLucenyType.Translucence:                              //半透明， 不能穿透
                _GoMaskPanel.SetActive(true);
                Color newColor2 = new Color(220, 220, 220, 50);
                _GoMaskPanel.GetComponent<Image>().color = newColor2;
                break;
            case UIFormLucenyType.ImPenetrable:                              //低透明，不能穿透
                _GoMaskPanel.SetActive(true);
                Color newColor3 = new Color(50, 50, 50, 200);
                _GoMaskPanel.GetComponent<Image>().color = newColor3;
                break;
            case UIFormLucenyType.Pentrate:                                  //可以穿透
                if (_GoMaskPanel.activeInHierarchy)
                {
                    _GoMaskPanel.SetActive(false);
                }
                break;
            default:
                break;
        }
        //遮罩窗体下移
        _GoMaskPanel.transform.SetAsLastSibling();
        //显示窗体下移
        goDisplarUIForms.transform.SetAsLastSibling();

    }

    /// <summary>
    /// 取消遮罩状态
    /// </summary>
    public void CancelMaskWindow()
    {
        //顶层窗口上移
        _GoTopPanel.transform.SetAsFirstSibling();
        //禁用遮罩窗体
        if (_GoMaskPanel.activeInHierarchy)
        {
            //隐藏
            _GoMaskPanel.SetActive(false);
        }
    }
}
